﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.AppLogic.BusinessEntities;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class UsersBLL:IDisposable
    {
        DatabaseContext _DatabaseContext;

        public UsersBLL():this(new DatabaseContext())
        { 
        }

        public UsersBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public void RegisterUser(string loginName, string password, string name, string email)
        {
            Users users = new Users();
            users.LoginName = loginName;
            users.Password = password;
            users.Name = name;
            users.Email = email;
            users.CreateDateTime = DateTime.Now;
            users.IsApproved = false;

            users = _DatabaseContext.Users.Add(users);
            _DatabaseContext.SaveChanges();

            UserRoles userRoleEntity = new UserRoles();
            userRoleEntity.RoleType = "科研提案人";
            userRoleEntity.UserID = users.ID;
            _DatabaseContext.UserRoles.Add(userRoleEntity);
            _DatabaseContext.SaveChanges();
        }

        public Users RegisterUser(Users registeredUser)
        {
            registeredUser.CreateDateTime = DateTime.Now;
            registeredUser.IsApproved = false;

            registeredUser = _DatabaseContext.Users.Add(registeredUser);
            _DatabaseContext.SaveChanges();

            UserRoles userRoleEntity = new UserRoles();
            userRoleEntity.RoleType = "科研提案人";
            userRoleEntity.UserID = registeredUser.ID;
            _DatabaseContext.UserRoles.Add(userRoleEntity);
            _DatabaseContext.SaveChanges();

            return registeredUser;
        }

        public Users GetUserByLoginName(string loginName)
        {
            return _DatabaseContext.Users.Where(u => u.LoginName.Equals(loginName) && !u.IsDeleted).FirstOrDefault();
        }

        public bool IsUserInfoComplete(Users userEntity)
        {
            return !string.IsNullOrEmpty(userEntity.Position) && !string.IsNullOrEmpty(userEntity.Gender);
        }

        public void ClearUserStudyAreas(Users userEntity)
        {
            List<UserStudyAreas> userStudyAreas = userEntity.UserStudyAreas.ToList();
            foreach (UserStudyAreas userStudyArea in userStudyAreas)
            {
                _DatabaseContext.UserStudyAreas.Remove(userStudyArea);
            }
            _DatabaseContext.SaveChanges();
        }

        public void AddUserStudyArea(UserStudyAreas userStudyArea)
        {
            _DatabaseContext.UserStudyAreas.Add(userStudyArea);
            _DatabaseContext.SaveChanges();
        }

        public void ClearUserRoles(Users userEntity)
        {
            List<UserRoles> userRoles = userEntity.UserRoles.ToList();
            foreach (UserRoles userRole in userRoles)
            {
                _DatabaseContext.UserRoles.Remove(userRole);
            }
            _DatabaseContext.SaveChanges();
        }

        public void AddUserRole(UserRoles userRole)
        {
            _DatabaseContext.UserRoles.Add(userRole);
            _DatabaseContext.SaveChanges();
        }

        public void Save()
        {
            _DatabaseContext.SaveChanges();
        }

        public IQueryable<Users> GetUserListWaitForChecking(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Users> users = _DatabaseContext.Users.Where(u => (!u.ApproveDateTime.HasValue) && u.DepartmentID != null);
            totalRowCount = users.Count();
            return users.OrderBy(u => u.ID).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Users> GetUserListManage(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
        {
            IQueryable<Users> users = _DatabaseContext.Users.Where(u => (u.IsApproved.HasValue && u.IsApproved.Value));
            totalRowCount = users.Count();
            return users.OrderBy(u => u.ID).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<Users> GetUsersByID(long userID)
        {
            return _DatabaseContext.Users.Where(u => u.ID == userID);
        }

        public IQueryable<UserRoles> GetUserRolesByLoginName(string loginName)
        {
            return _DatabaseContext.UserRoles.Where(u => u.Users.LoginName.Equals(loginName));
        }

        public IQueryable<UserRoles> GetUserRolesByUserID(long userID)
        {
            return _DatabaseContext.UserRoles.Where(u => u.Users.ID.Equals(userID));
        }

        public IQueryable<Users> GetUsersNeedToTransfer(long motionID)
        {
            Motions motionEntiy = _DatabaseContext.Motions.Where(m => m.ID == motionID).FirstOrDefault();
            string role = "";
            switch (motionEntiy.Status)
            {
                case "等待转发专家审核":
                    role = "评审专家";
                    break;
                case "等待转发部门领导审核":
                    role = "部门负责人";
                    break;
                case "等待转发校领导审核":
                    role = "校领导";
                    break;
            }
            return _DatabaseContext.Users.Where(u => u.ID != motionEntiy.ProposerID && !u.IsDeleted && u.UserRoles.Where(r => r.RoleType.Equals(role)).Count() > 0);
        }

        public List<AssignPerson> GetAssignPersonByRole(string role)
        {
            List<Users> userList = _DatabaseContext.Users.Where(u => u.UserRoles.Where(r => r.RoleType.Equals(role)).Count() > 0).ToList();
            List<AssignPerson> assignPersonList = new List<AssignPerson>();
            foreach (Users user in userList)
            {
                AssignPerson assignPerson = new AssignPerson();
                assignPerson.ID = user.ID;
                assignPerson.Text = (user.Name.Length >= 4? user.Name : user.Name.PadRight(4-user.Name.Length)) + " 部门： " + (user.Departments.Name.Length >= 10? user.Departments.Name: user.Departments.Name.PadRight(10-user.Departments.Name.Length)) 
                    + " " + (user.SecondDepartments.Name.Length >= 20? user.SecondDepartments.Name: user.SecondDepartments.Name.PadRight(20-user.SecondDepartments.Name.Length)) + " 研究领域:";
                foreach (UserStudyAreas studyArea in user.UserStudyAreas)
                {
                    assignPerson.Text += studyArea.StudyAreas.Name + " "; 
                }
                assignPersonList.Add(assignPerson);
            }
            return assignPersonList;
        }


    }
}